package com.monkeyboy.service;

import com.monkeyboy.common.ScheduleConstants;
import com.monkeyboy.common.dto.ResData;
import com.monkeyboy.common.utils.job.ScheduleUtils;
import com.monkeyboy.common.utils.poi.ExcelUtil;
import com.monkeyboy.dao.BaseDao;
import com.monkeyboy.model.SysJob;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Gavin
 * @date 2020.07.07 18:23
 */
@Service
public class SystemJobService {
    @Autowired
    private BaseDao bd;
    @Autowired
    private Scheduler scheduler;

    public ResData getJobList() throws Exception {
        List<SysJob> jobList = (List<SysJob>) bd.findForList("SystemJobMapper.getAllSystemJob", null);
        return ResData.SUCCESS_DATA(jobList);
    }

    /**
     * 改变定时任务状态
     *
     * @param job
     * @return
     * @throws Exception
     */
    @Transactional
    public ResData changeStatus(SysJob job) throws Exception {
        int rows = 0;
        String status = "";
        if ("0".equals(job.getStatus())) {
            status = "1";
        } else {
            status = "0";
        }
        job.setStatus(status);
        if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {//如果status=0就恢复定时任务状态
            rows = resumeJob(job);
        } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {//如果status=1就停止定时任务
            rows = pauseJob(job);
        }
        return ResData.SUCCESS_DATA(rows);
    }

    /**
     * 恢复定时任务
     *
     * @param job 调度信息
     */
    @Transactional
    public int resumeJob(SysJob job) throws Exception {
        Integer jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        int rows = bd.update("SystemJobMapper.updateJob", job);
        if (rows > 0) {//恢复定时任务
            scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup));
        }
        return rows;
    }

    /**
     * 暂停任务
     *
     * @param job 调度信息
     */
    @Transactional
    public int pauseJob(SysJob job) throws Exception {
        Integer jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = bd.update("SystemJobMapper.updateJob", job);
        if (rows > 0) {
            scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
        }
        return rows;
    }

    /**
     * 新增一个定时任务，并添加到quartz维护的数据库定时任务列表里面
     */
    public ResData insertOneJob(SysJob job) throws Exception {
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = bd.save("SystemJobMapper.insertJob", job);
        if (rows > 0) {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
        return ResData.SUCCESS();
    }

    public ResData export(SysJob sysJob) throws Exception {
        List<SysJob> jobList = (List<SysJob>) bd.findForList("SystemJobMapper.selectJobVo", sysJob);
        if (jobList == null || jobList.size() == 0) {
            return ResData.ERROR_500000("暂无定时任务数据。");
        }
        ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);

        return util.exportExcel(jobList, "定时任务");
    }
}
